|
|
|
|
|
|
|
the solution to the original equations. Dividing by the first entry in the first row by itself makes that entry 1. |
|
|
|
|
|
|
|
|
Next, for each row i, except the first, subtract Ci0 times the first row's values. At this point, the first entry in the first row is 1, so subtracting Ci0 times 1 from Ci0 makes the first entry in the ith row 0. The other entries have other values depending on the values in the other columns. For example, the entry in the jth column of the ith row will have value Cij Ci0 * C10. |
|
|
|
|
|
|
|
|
After you have finished this step, the first row has a 1 in its first column and all the other rows have 0s in their first columns. |
|
|
|
|
|
|
|
|
Now repeat these steps for the second column using the second row. Divide each value in the second row by C11 to make the second entry in the second row 1. Then subtract multiples of the second row from every other row to make all the other entries in the second column 0. |
|
|
|
|
|
|
|
|
There is one other strange case that may arise. When the program should convert the entry Cii to 1 and use it to eliminate the ith entries in the other rows, the value Cii may be 0. In that case, it cannot be used to eliminate the other entries in its columns. |
|
|
|
|
|
|
|
|
When this happens, the program should search the remaining rows and find one with a nonzero entry in this column. It should swap the two rows and continue. This corresponds to switching the order of two of the original equations and does not change the eventual solution. |
|
|
|
|
|
|
|
|
Continue this process for each column until the array looks like this: |
|
|
|
|
|
|
|
|
The ith row in this array corresponds to the equation: |
|
|
|
 |
|
|
|
|
A0 * 0 +A1 * 0 + . . . + Ai * 1 + . . . + AN * 0 = Di |
|
|
|
|
|
|
|
|
From this equation, it is easy to see that Ai = Di. |
|
|
|
|
|
|
|
|
Example program LeastSq3, shown in Figure 11.11, uses this method for finding higherorder polynomial least square curves. Click several times to select points and enter the degree you want for the polynomial. When you click the Go button, the program finds a polynomial to approximate the points you selected. |
|
|
|
|
|
|
|
|
The following code shows how program LeastSq3 works. Subroutine DrawCurve orchestrates the process and draws the final curve. It calls GetLeastSquareValues to calculate the polynomial's coefficients. It then uses the PolynomialValue function to compute the coordinates of the points on the curve. |
|
|
|
|
|